#pragma once
#include<iostream>
#include<string>
#include<ctime>
#include <cstdarg>
#include<unistd.h>

#define LOG_NORMAL "log.txt"
#define LOG_ERR "err.txt"
using namespace std;

#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

const char* to_levelstr(int level)
{  
   if(level==DEBUG)return "DEBUG";
   if(level==NORMAL)return "NORMAL";
   if(level==WARNING)return "WARNING";
   if(level==ERROR)return "ERROR";
   if(level==FATAL)return "FATAL";
   return nullptr;
}





void logmessage(int level,const char*format...)
{    char buffer[2048];
    snprintf(buffer,sizeof(buffer),"[%s][%d][pid:%d]",to_levelstr(level),(unsigned int)time(nullptr),getpid());
    char  logbuffer[2048];
    va_list ag;
    va_start(ag,format);
    vsnprintf(logbuffer,sizeof(logbuffer),format,ag);
    FILE*log=fopen(LOG_NORMAL,"a");
    FILE*err=fopen(LOG_ERR,"a");
    if(log!=nullptr&&err!=nullptr)
    {   FILE*curr=nullptr;
        if(level==DEBUG||level==NORMAL||level==WARNING)curr=log;
        if(level==FATAL||level==ERROR)curr=err;
        if(curr!=nullptr)
          {

            fprintf(curr,"%s%s\n",buffer,logbuffer);
          }
          fclose(log);
          fclose(err);
    }
   



}